﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;

namespace WuxiWTO.Web.backend
{
    public partial class imageNewsList : BasePage
    {
        WTODataContext contex = new WTODataContext();
        PagedDataSource psd = new PagedDataSource();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            AspNetPager1.RecordCount = contex.image_news.Count();
            var list = from c in contex.image_news orderby c.update_date descending select c;
            psd.DataSource = list.ToList();
            psd.AllowPaging = true;
            psd.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            psd.PageSize = AspNetPager1.PageSize;

            this.rptLinks.DataSource = psd;
            this.rptLinks.DataBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void rptLinks_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var item = e.Item.DataItem;
                image_new cat = item as image_new;

                LinkButton button = e.Item.FindControl("btnDelete") as LinkButton;
                Label recordNo = e.Item.FindControl("lblNo") as Label;
                Label title = e.Item.FindControl("lblTitle") as Label;
                recordNo.Text = (e.Item.ItemIndex + 1 + (AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1))).ToString();
                title.Text = Utility.CutStringByLength(cat.news_title, 20);
                button.Attributes.Add("onclick", "return confirm('您确定要删除该友情链接吗？');");
            }
        }
        protected void rptLinks_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DeleteCate")
            {
                int link_id = Convert.ToInt32(e.CommandArgument);
                image_new cat = contex.image_news.SingleOrDefault(p => p.ID == link_id);
                if (cat != null)
                {
                    contex.image_news.DeleteOnSubmit(cat);
                    contex.SubmitChanges();
                }

                if (!string.IsNullOrEmpty(cat.image_path))
                {
                    string filePath =Server.MapPath("~/" + cat.image_path);
                    string originalFilePath = Server.MapPath("~/" + cat.image_path.Replace("thumb_", ""));
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }
                    if (System.IO.File.Exists(originalFilePath))
                    {
                        System.IO.File.Delete(originalFilePath);
                    }
                }

                BindGrid();
            }
        }
    }
}